package com.zara.store.client.clientmodel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RetailBasket implements Basket {
	private List<BasketLineModel> lines = new ArrayList<BasketLineModel>();
	
	private int rowIndex;

	private BasketLineModel line;

	public BigDecimal getTotal() {
		BigDecimal total = BigDecimal.ZERO;
		for(BasketLineModel l: lines) {
			total = total.add(l.getLineTotal());
		}
		return total;
	}

	public void removeLine(int i) {
		setRowIndex(i);
		lines.remove(rowIndex);
	}
	public void setLineAmount(BigDecimal amount) {
		line.setAmount(amount);
	}
	public void setLineOffer(boolean offer) {
		line.setOffer(offer);
	}
	public boolean getLineOffer() {
		return line.getOffer();
	}
	public String getLineProduct() {
		return line.getProductName();
	}
	public BigDecimal getLineAmount() {
		return line.getAmount();
	}
	public BigDecimal getLineTotal() {
		return line.getLineTotal();
	}
	public BigDecimal getLinePrice() {
		return line.getPrice();
	}
	public int size() {
		return lines.size();
	}
	public int getRowIndex() {
		return rowIndex;
	}
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}
	public void changeRowIndex(int rowIndex) {
		setRowIndex(rowIndex);
		this.line = lines.get(rowIndex);
	}
	public List<BasketLine> getElements() {
		return Collections.unmodifiableList(new ArrayList<BasketLine>(lines));
	}
	public void addProduct(RetailProduct productModel) {
		if(productModel.hasProduct()) {
			for(int i=0; i<lines.size(); i++) {
				BasketLine l = lines.get(i);
				if(l.getProductId()==productModel.getProductVO().getId()) {
					//si tengo el producto
					changeRowIndex(i);
					setLineAmount(getLineAmount().add(BigDecimal.ONE));
					return;
				}
			}
			setRowIndex(lines.size());
			line = new BasketLineModel(productModel);
			lines.add(line);
		}
	}
	public boolean hasOfferPrice() {
		return line.hasOfferPrice();
	}
	public boolean hasLine() {
		return line != null;
	}
}
